/**
 * Copyright (c) 2016-2019 bootwaf开源 All rights reserved.
 *
 * http://www.ihuanzhi.com
 *
 * 版权所有，侵权必究！
 */

package com.casic.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author XDZ 1136277749@qq.com
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public enum RCode {
		//成功
		SUCCESS(0, "成功"),		
		//没有权限
		NO_POWER(301,"没有权限"), 
		//未知错误
		UNKNOWN_ERROR(500, "未知异常，请联系管理员"),
		//未登录
		UN_LOGIN(401,"未登录"),

		PARAMETER_IS_INCORRECT(401,"parameter is incorrect"), //参数不完整
		EXCEPTION_FOR_CODE(101,"abnormal occurrence"),  //发生异常使用
		PARAMETR_TO_MANY(402,"parameter too many"),
		PARAMETER_VALID_FALIURE(403,"parameter valid faliure"),
		NO_TRADERS(404,"no traders"),  //没有使用商
		NO_CACHE_IN_MEMCACHE(405,"no this cache in memcache"),
		NO_DATE_IN_MONGODB(411,"no this data in mongodb"),
		NOT_ALLOWED_TO_USE(406,"this interface is not allowed to use. please contact the administrator."),
		NO_CACHE_IN_REDIS(407,"no this cache in redis"),
		NO_CORRESPONDING_DEVICE_DEALER(408,"no corresponding device dealer"),
		NO_equipment_dealer(409,"No corresponding equipment dealer"),
		VALIDATA_FALIURE(410,"no flie or suffix name is not qualified"),
		DEVICETYPE_NOT_EXIST(302,"deviceType not exist"),
		DEVICE_NOT_EXIST(301,"device not exist"),
		DEVICEGROUP_NOT_EXIST(303,"deviceGroup not exist"),
		GATEWAY_NOT_EXIST(304,"gateway not exist"),
		IDENTITY_NOT_EXIST(305,"identity not exist"),
		ORG_NOT_EXIST(306,"org not exist"),
		COLLPOINT_NOT_EXIST(307,"collpoint not exist"),
		NAME_REPEAT(308,"name repeat"),
		MODEL_REPEAT(314,"model repeat"),
		KEYNAME_REPEAT(309,"keyname repeat"),
		AGREEMENT_IDENTITY_NOT_EXIST(310,"this agreement identity not exist"),
		ENUM_NOT_EXIST(311,"enum not exist"),
		DEVICE_DEVNUM_REPEAT(312,"devnum repeat"),
		GATEWAY_HAS_DEVICE(313,"gateway_has_devices"),
		ERROR_CODE(1,"not exist enum code"),
		FAILURE(100,"failure");

		//返回码
		int code;
		//返回信息
		String msg;

		private RCode(int code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		public Integer getCode() {
			return code;
		}
		
		public String getMsg() {
			return msg;
		}
	}
	
	public R() {
		put("code", RCode.SUCCESS.code);
		put("msg", RCode.SUCCESS.msg);
	}
	
	public static R error() {
		return error(RCode.UNKNOWN_ERROR, RCode.UNKNOWN_ERROR.msg);
	}
	
	public static R error(String msg) {
		return error(RCode.UNKNOWN_ERROR, msg);
	}
	
	public static R error(RCode rcode, String msg) {
		R r = new R();
		r.put("code", rcode.code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}
	
	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}
	
	public static R ok() {
		return new R();
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
